﻿#include "widget.h"
#include "ui_widget.h"
#include <qDebug>
#include <QFileDialog>
#include <QSqlRecord>

//测试修改 测试啊啊啊
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
    isPlay = 0; //播放标志位 0是未播放 1是播放状态
    //初始化
    qw = ui->musiclist; //音乐列表对象
    dbt = new dbtools();    //数据库工具类
    query = dbt->CreatConnection();
    if(!query)
    {
        qDebug()<<"链接或者创建数据库失败了";
    }

    Player = new QMediaPlayer();    //播放器对象
    PlayList = new QMediaPlaylist();    //播放列表对象
    Player->setPlaylist(PlayList);  //设置播放列表

    dbt->ReadList(qw,PlayList); //从数据库里面读取音乐列表
    play_function = 0;
    //链接专区
    connect(Player,&QMediaPlayer::positionChanged,this,&Widget::updatePosition);    //歌曲播放进度
    connect(Player,&QMediaPlayer::durationChanged,this,&Widget::updateDuration);    //切歌

}

Widget::~Widget()
{
    delete ui;
}

//退出按钮
void Widget::on_Close_clicked()
{
    exit(0);
}

//添加音乐
void Widget::on_addMusic_clicked()
{

}

//添加音乐文件夹
void Widget::on_addFiles_clicked()
{
   QString directory = QFileDialog::getExistingDirectory(this,tr("获取文件夹"),"./");
   if (!directory.isEmpty())
   {
       musicFiles = directory;
       musiclist_v = t.GetFileList(musicFiles); //获取文件夹里面的音乐列表
   }
   dbt->ReadList(qw,PlayList); //从数据库里面读取音乐列表
}

void Widget::on_musiclist_doubleClicked(const QModelIndex &index)
{
    int currenRow = ui->musiclist->currentRow();//当前单击选中ListWidget控件的行号（第几行）
    qDebug()<<tr("%1").arg(currenRow);
    //listPlay(currenRow,ui->MusicTitle,ui->musiclist,ui->play);

    PlayList->setCurrentIndex(currenRow);
    Player->play();
    qDebug()<<"播放开始: "<< PlayList->mediaCount()<<"\n"<<PlayList->currentIndex()<<" "<<PlayList->error();
    ui->MusicTitle->setText(qw->item(PlayList->currentIndex())->text());
    ui->play->setStyleSheet("border-image:url(:/image/image/widget/noplay.png);QPushButton::hover{background-color:rgba(255,255,255,0.3);}");

}

void Widget::on_play_clicked()
{

    if(isPlay == 1)
    {
        ui->play->setStyleSheet("#play{border-image:url(:/image/image/widget/play.png);}#play:hover{background-color:rgba(255,255,255,0.3);}");
        isPlay = 0; //设置播放状态
        Player->pause();
        qDebug()<<"暂停";
    }
    else if(isPlay == 0)
    {
        ui->play->setStyleSheet("#play{border-image:url(:/image/image/widget/noplay.png);}#play:hover{background-color:rgba(255,255,255,0.3);}");
        isPlay = 1; //设置播放状态
        Player->play();
        qDebug()<<"播放";
    }
//    Player->play();
    //qDebug()<<"播放开始: "<< PlayList->mediaCount()<<"\n"<<PlayList->currentIndex();

//调试播放代码
//    QMediaPlayer *player2 = new QMediaPlayer;
//    player2->setMedia(QMediaContent(QUrl::fromLocalFile("G:/CloudMusic/a.mp3")));
//    player2->play();
}

void Widget::on_last_clicked()
{
    int row = PlayList->mediaCount();//获取歌曲数量
    int current  = PlayList->currentIndex();//获取当前位置
    if(--current<0){
    current=0;
    }
    PlayList->setCurrentIndex(current);//设置当前音乐
    qw->scrollToItem(qw->item(current));//滚动到当前音乐
    qDebug()<<"当前: " + current;
    Player->play();//播放
    ui->MusicTitle->setText(qw->item(PlayList->currentIndex())->text());
}

void Widget::on_next_clicked()
{
    int row = PlayList->mediaCount();//获取歌曲数量
    int current  = PlayList->currentIndex();//获取当前位置
    if(++current>row)
    {
        current=0;
    }
    PlayList->setCurrentIndex(current);//设置当前音乐
    qw->scrollToItem(qw->item(current));//滚动到当前音乐
    qDebug()<<current;
    Player->play();//播放
    ui->MusicTitle->setText(qw->item(PlayList->currentIndex())->text());
}


void Widget::updatePosition(qint64 position)//接收歌曲位置改变，那么滑块的位置也要变
{
    ui->Slider_jindu->setValue(position);//设置滑块位置
    ui->cur_time->setText(t.ToTime(position)+"/");
}

void Widget::updateDuration(qint64 duration)//更新播放歌曲的时间
{
    ui->Slider_jindu->setRange(0,duration);//根据播放时长来设置滑块的范围
    ui->Slider_jindu->setEnabled(duration>0);
    ui->Slider_jindu->setPageStep(duration/10);//以及每一步的步数
    ui->time_len->setText(t.ToTime(duration));
}

void Widget::on_Slider_jindu_sliderPressed()
{

}

void Widget::on_Slider_jindu_valueChanged(int value)
{
    //qDebug()<<value;
    //Player->setPosition(value);
}

void Widget::on_Slider_jindu_actionTriggered(int action)
{

}


void Widget::on_playcontain_clicked()   //切换播放模式 0=单曲 1=单曲循环 2=顺序播放 3=列表循环 4=随机播放
{
    play_function = PlayList->playbackMode();
    play_function=play_function%4;
    PlayList->setPlaybackMode((QMediaPlaylist::PlaybackMode)++play_function);
    qDebug()<<play_function;
    if(play_function == 1)
    {
        ui->playcontain->setStyleSheet("#playcontain{border-image:url(:/image/image/widget/loop.png);}#playcontain:hover{background-color:rgba(255,255,255,0.3);}");
    }
    if(play_function == 2)
    {
        ui->playcontain->setStyleSheet("#playcontain{border-image:url(:/image/image/widget/listplay.png);}#playcontain:hover{background-color:rgba(255,255,255,0.3);}");
    }
    if(play_function == 3)
    {
        ui->playcontain->setStyleSheet("#playcontain{border-image:url(:/image/image/widget/listloop.png);}#playcontain:hover{background-color:rgba(255,255,255,0.3);}");
    }
    if(play_function == 4)
    {
        ui->playcontain->setStyleSheet("#playcontain{border-image:url(:/image/image/widget/random.png);}#playcontain:hover{background-color:rgba(255,255,255,0.3);}");
    }
}
